﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// List of the forums.  This follows the singleton pattern.
/// </summary>
public class ForumList
{
    private static ForumList forumList;
    private ForumDatabaseAdapter databaseAdapater;
    private List<Forum> forums;

	protected ForumList()
	{
        forums = new List<Forum>();
	}

    public List<Forum> Forums
    {
        get { return this.forums; }
        set { this.forums = value; }
    }

    public static ForumList GetForumListObject()
    {
        if (forumList == null)
            forumList = new ForumList();

        return forumList;
    }

    public List<Forum> GetForums(string tableName)
    {
        this.forums.Clear();

        try
        {
            RetrieveForums(tableName);
        }
        catch (Exception ex) { }
 
        return this.forums;
    }

    private List<Forum> RetrieveForums(string tableName)
    {
        databaseAdapater = new ForumDatabaseAdapter();

        databaseAdapater.Open();

        if (databaseAdapater.DoesTableExist(tableName))
        {
            SqlDataReader reader = databaseAdapater.GetEntries(tableName);

            while (reader.Read())
            {
                Forum forum = new Forum(reader.GetString(0), reader.GetString(1), reader.GetString(2));
                forums.Add(forum);
            }
        }
        databaseAdapater.Close();

        return this.forums;
    }

    /// <summary>
    /// Get all the forums from the singleton
    /// </summary>
    /// <param name="forumName"></param>
    /// <returns></returns>
    public Forum RetrieveForumObject(string forumName)
    {
        for(int i=0; i < this.forums.Count; i++)
            if (this.forums[i].ForumName.CompareTo(forumName) == 0)
                return forums[i];

        return null;
    }

}